package com.huhu.levelloader;

import java.util.ArrayList;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.tmx.TMXObject;
import org.andengine.extension.tmx.TMXObjectGroup;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.huhu.interfaces.IPlayer;
import com.huhu.manager.SceneManager;
import com.huhu.objects.Player;
import com.huhu.scenes.GameScene;

public class LevelLoader1_6 extends LevelLoader{
	private HUD gameHUD;
	private Player player;

	public LevelLoader1_6(Engine engine, GameScene scene, BoundCamera camera,
			PhysicsWorld physicsWorld, VertexBufferObjectManager vbom) {
		super(engine, scene, camera, physicsWorld, vbom);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void createHUD() {
		gameHUD = new HUD();

		final Sprite spriteMoveLeft = new Sprite(10, 520,
				resourcesManager.move_left_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					player.setDirection(false);
					player.setRunning();
				} else {
					player.setStopping();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteMoveLeft);
		gameHUD.attachChild(spriteMoveLeft);

		final Sprite spriteMoveRight = new Sprite(135, 520,
				resourcesManager.move_right_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					player.setDirection(true);
					player.setRunning();
				} else {
					player.setStopping();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteMoveRight);
		gameHUD.attachChild(spriteMoveRight);

		final Sprite spriteJump = new Sprite(876, 520,
				resourcesManager.jump_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					player.jump();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteJump);
		gameHUD.attachChild(spriteJump);

		final Sprite spriteBack = new Sprite(10, 10,
				resourcesManager.back_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteBack);
		gameHUD.attachChild(spriteBack);

		camera.setHUD(gameHUD);
		
	}

	@Override
	public IPlayer createPlatform(TMXTiledMap mTMXTiledMap) {
		final ArrayList<TMXObjectGroup> mTMXObjectGroups = mTMXTiledMap.getTMXObjectGroups();
		final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f);
		for(TMXObjectGroup mTMXObjectGroup : mTMXObjectGroups){
			if(mTMXObjectGroup.getTMXObjectGroupProperties().containsTMXProperty("platformobject", "true")){
				for(TMXObject mTMXObject : mTMXObjectGroup.getTMXObjects()){
					final Rectangle rectangle = new Rectangle(mTMXObject.getX(), mTMXObject.getY(), mTMXObject.getWidth(), mTMXObject.getHeight(), vbom);
					final Body body_platform = PhysicsFactory.createBoxBody(physicsWorld, rectangle, BodyType.StaticBody, FIXTURE_DEF);
					body_platform.setUserData("body_platform_data");
					rectangle.setVisible(false);
					scene.attachChild(rectangle);
				}
			} else if(mTMXObjectGroup.getTMXObjectGroupProperties().containsTMXProperty("object", "true")){
				for(TMXObject mTMXObject : mTMXObjectGroup.getTMXObjects()){
					final float x = mTMXObject.getX();
					final float y = mTMXObject.getY();
					final float width = mTMXObject.getWidth();
					final float height = mTMXObject.getHeight();
					
					if(mTMXObject.getName().equals("box1")){
						
					} else if(mTMXObject.getName().equals("box2")){
						
					} else if(mTMXObject.getName().equals("box3")){
						
					} else if(mTMXObject.getName().equals("box1")){
						
					} else if(mTMXObject.getName().equals("player")){
						
					}
				}
			}
		}
		
		
		return player;
	}

	@Override
	protected ContactListener contactListener() {
		final ContactListener contactListener = new ContactListener() {
			
			@Override
			public void preSolve(Contact contact, Manifold oldManifold) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void postSolve(Contact contact, ContactImpulse impulse) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void endContact(Contact contact) {
				final Fixture x1 = contact.getFixtureA();
				final Fixture x2 = contact.getFixtureB();
				final Object x1_bodyData = x1.getBody().getUserData();
				final Object x2_bodyData = x2.getBody().getUserData();
				
				if(x1_bodyData != null || x2_bodyData != null){
					if(x1_bodyData.equals("player") || x2_bodyData.equals("player")){
						player.decreaseFootContacts();
					}
				}
			}
			
			@Override
			public void beginContact(Contact contact) {
				final Fixture x1 = contact.getFixtureA();
				final Fixture x2 = contact.getFixtureB();
				final Object x1_bodyData = x1.getBody().getUserData();
				final Object x2_bodyData = x2.getBody().getUserData();
				
				if(x1_bodyData.equals("player") || x2_bodyData.equals("player")){
					player.increaseFootContacts();
					
					
				}
				
			}
		};
		return contactListener;
	}

}
